package main

import "fmt"


func shiftGrid(grid [][]int, k int) [][]int {
    // 二维变一维
	m,n := len(grid),len(grid[0])
    nums := make([]int, m*n)
    idx := 0
    for i:=range grid {
        for j:=range grid[i] {
            nums[idx] = grid[i][j]
            idx++
        }
    }
    // 一维移动k
    // 1. 复制两倍
    nums = append(nums, nums...)
	// 2. 截取
	k = k%(m*n)
	st,end := (m*n-k), m*n*2-k
	nums = nums[st:end]
	// 3. 1d->2d
	idx = 0
	for i:=0;i<m;i++{
		for j:=0;j<n;j++{
			grid[i][j] = nums[idx]
			idx ++
		}
	}
	return grid
}

func main(){
	grid:=[][]int{
		{1,2,3},{4,5,6},{7,8,9},
	}
	res := shiftGrid(grid,1)
	fmt.Println(res)
}